import request from './index.js'

export const addressApi = {
    list: () => {
        console.log('调用地址列表API');
        return request({ url: '/address', method: 'get' })
            .then(res => {
                console.log('地址列表API返回:', JSON.stringify(res));
                return res;
            })
            .catch(err => {
                console.error('地址列表API错误:', err);
                throw err;
            });
    },
    get: (uuid) => {
        console.log('获取单个地址，ID:', uuid);
        return request({ url: `/address/${uuid}`, method: 'get' })
            .then(res => {
                console.log('获取地址API返回:', JSON.stringify(res));
                return res;
            });
    },
    add: (data) => {
        console.log('添加地址，数据:', JSON.stringify(data));
        return request({ url: '/address', method: 'post', data })
            .then(res => {
                console.log('添加地址API返回:', JSON.stringify(res));
                return res;
            })
            .catch(err => {
                console.error('添加地址API错误:', err);
                throw err;
            });
    },
    update: (uuid, data) => {
        console.log('更新地址，数据:', JSON.stringify(data));
        return request({
            url: `/address/${uuid}`,
            method: 'put',
            data
        })
            .then(res => {
                console.log('更新地址API返回:', JSON.stringify(res));
                return res;
            })
            .catch(err => {
                console.error('更新地址API错误:', err);
                throw err;
            });
    },
    delete: (uuid) => {
        return request({
            url: `/address/${uuid}`,
            method: 'delete'
        })
    },
    setDefault: (uuid) => {
        return request({
            url: `/address/default/${uuid}`,
            method: 'put'
        })
    },
    getRegions: () => request({ url: '/address/regions', method: 'get' })
}; 